// mircoApp/agentShop/agentShop_apply/agentShop_apply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headTitle: "推客店铺申请",
    showAgreement: false,
    // 时间弹窗开关
    show: false,
    checked: false,

    //1开始时间 2结束时间
    showType: 1,
    startTime: "",
    endTime: "",
    currentDate: new Date().getTime(),
    minDate: "",
    maxDate: "",

    // 静态图片
    imgWaiting: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FagentShop%2F%E7%AD%89%E5%BE%85%E5%AE%A1%E6%A0%B8%402x.png",
    imgFail: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FagentShop%2F%E7%AD%89%E5%BE%85%E5%AE%A1%E6%A0%B8%402x%20(1).png",

    // 图片回显
    id_card_font: "",
    id_card_back: "",

    // 提交数据格式
    form: {
      avatar: "",
      nickname: "",
      mobile: "",
      shop_type: "1",
      shop_name: "",
      shop_short_name: "",
      shop_desc: "",
      id_card_name: "",
      id_card_number: "",
      id_card_font: "",
      id_card_back: "",
      id_card_start_day: "",
      id_card_end_day: "",
      is_long: "0"
    },
    infoData: {
      // 0 推客店铺图片立即申请 1申请中待审核 2申请被驳回 3已经拥有店铺无需申请
      status: 3
    },
    isApplyOpen: false,
    isclick: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let year = new Date().getFullYear();
    this.setData({
      minDate: new Date(year - 90, 1, 1).getTime(),
      maxDate: new Date(year + 90, 1, 1).getTime()
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  closePopup() {
    this.setData({
      showAgreement: false
    })
  },
  toAgreement() {
    this.setData({
      showAgreement: true
    })
  },
  radioChange_long(e) {
    this.setData({
      'form.is_long': e.detail
    })
  },
  radioChange(e) {
    this.setData({
      'form.shop_type': e.detail
    })
  },
  cancel() {
    this.setData({
      show: false
    })
  },
  onConfirm(e) {
    let timeS = e.detail;
    if (this.data.showType == 1) {
      this.setData({
        'form.id_card_start_day': this.dateFormat(timeS, 'YMD'),
        startTime: this.dateFormat(new Date(timeS).getTime(), 'Y/M/D')
      })
    } else {
      this.setData({
        'form.id_card_end_day': this.dateFormat(timeS, 'YMD'),
        endTime: this.dateFormat(new Date(timeS).getTime(), 'Y/M/D')
      })
    }
    this.setData({
      show: false,
      currentDate: new Date(timeS).getTime()
    })
  },
  dateFormat(number, format) {
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];
    var date = new Date(number);
    returnArr.push(date.getFullYear());
    returnArr.push(this.formatNumber(date.getMonth() + 1));
    returnArr.push(this.formatNumber(date.getDate()));
    returnArr.push(this.formatNumber(date.getHours()));
    returnArr.push(this.formatNumber(date.getMinutes()));
    returnArr.push(this.formatNumber(date.getSeconds()));
    for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
  },
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  showTypeBtn(e) {
    let {
      key
    } = e.currentTarget.dataset;
    if (this.data.form.is_long == "1" && key == 2) {
      // 长期不选择结束时间
      return;
    }
    this.setData({
      currentDate: new Date().getTime(),
      showType: key,
      show: true
    })
  },
  async applyPost() {
    console.log(this.data.form)
    if (app._isTextEmpty(this.data.form.shop_name)) {
      app.tips("请输入店铺名称");
      return;
    }
    if (app._isTextEmpty(this.data.form.id_card_name)) {
      app.tips("请输入身份证姓名");
      return;
    }
    if (app._isTextEmpty(this.data.form.id_card_number)) {
      app.tips("请输入身份证号码");
      return;
    }
    if (app._isTextEmpty(this.data.form.id_card_font)) {
      app.tips("请上传身份证正面图片");
      return;
    }
    if (app._isTextEmpty(this.data.form.id_card_back)) {
      app.tips("请上传身份证反面图片");
      return;
    }
    if (app._isTextEmpty(this.data.form.id_card_start_day)) {
      app.tips("请选择身份证有效期开始日期");
      return;
    }
    if (app._isTextEmpty(this.data.form.id_card_end_day) && this.data.form.is_long != "1") {
      app.tips("请选择身份证有效期截止日期");
      return;
    }
    if (this.data.form.shop_type == 2 && app._isTextEmpty(this.data.form.shop_short_name)) {
      app.tips("请输入商户简称");
      return;
    }
    if (!this.data.checked) return app.tips("请勾选店铺协议");
    if (this.data.isclick) return;
    this.data.isclick = true;
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.agent-shop.frontend.apply.apply"),
      data: this.data.form
    })
    if (!data.result) {
      app.tips(data.msg)
      this.data.isclick = false
      return
    }
    setTimeout(() => {
      this.getData();
      this.data.isclick = false
    }, 800);
  },
  confirmBtn() {
    if (this.data.infoData.status == 2) {
      this.setData({
        'infoData.status': 0
      })
      console.log(this.infoData);
      return;
    }
    wx.redirectTo({
      url: '/packageG/member_v2/member_v2',
    })
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  inpVal(e) {
    let key = e.currentTarget.dataset.key;
    let name = `form.${key}`;
    this.setData({
      [name]: e.detail.value
    })
  },
  afterRead_t(event) {
    wx.showLoading({
      title: '上传中',
    })
    let {
      file
    } = event.detail;
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      formData: null,
      success: (res) => {
        wx.hideLoading();
        let _res = JSON.parse(res.data);
        this.setData({
          'form.avatar': _res.data.img,
          avatar: _res.data.img_url
        })
      },
      fail: (err) => {
        wx.hideLoading()
      }
    });
  },
  //头像上传
  afterRead_z(event) {
    wx.showLoading({
      title: '上传中',
    })
    let {
      file
    } = event.detail;
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      formData: null,
      success: (res) => {
        wx.hideLoading();
        let _res = JSON.parse(res.data);
        this.setData({
          'form.id_card_font': _res.data.img,
          id_card_font: _res.data.img_url
        })
      },
      fail: (err) => {
        wx.hideLoading()
      }
    });
  },
  //头像上传
  afterRead_f(event) {
    wx.showLoading({
      title: '上传中',
    })
    let {
      file
    } = event.detail;
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      formData: null,
      success: (res) => {
        wx.hideLoading();
        let _res = JSON.parse(res.data);
        this.setData({
          'form.id_card_back': _res.data.img,
          id_card_back: _res.data.img_url
        })
      },
      fail: (err) => {
        wx.hideLoading()
      }
    });
  },
  isStoreOpen() {
    this.setData({
      isApplyOpen: true
    })
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.agent-shop.frontend.apply.index"),
    })
    if (!data.result) {
      app.tips(data.msg)
      return
    }
    let res = data.data;
    this.setData({
      infoData: res
    })
    if (!res.shop) {
      this.setData({
        isApplyOpen: false
      })
    }
    if (res.member && this.data.infoData.apply != 2) {
      this.setData({
        'form.avatar': res.member.avatar_image,
        avatar: res.member.avatar_image,
        'form.nickname': res.member.nickname,
        'form.shop_name': res.member.nickname,
        'form.mobile': res.member.mobile,
      })
    }
    if (res.apply && this.data.infoData.status == 2) {
      this.setData({
        'form.id_card_back': res.apply.id_card_back,
        id_card_back: res.apply.id_card_back_image,
        'form.id_card_font': res.apply.id_card_font,
        id_card_font: res.apply.id_card_font_image,
        'form.id_card_name': res.apply.id_card_name,
        id_card_number: res.apply.id_card_number,
        'form.shop_desc': res.apply.shop_desc,
        'form.avatar': res.apply.avatar,
        avatar: res.apply.avatar_image,
        'form.id_card_number': res.apply.id_card_number,
        'form.nickname': res.apply.shop_name,
        'form.shop_name': res.apply.shop_name,
        'form.mobile': res.apply.mobile,
        'form.shop_type': String(res.apply.shop_type),
        'form.is_long':String(res.apply.is_long)
      })
      if (res.apply.shop_type == "2") {
        this.setData({
          'form.shop_short_name': res.apply.shop_short_name
        })
      }
      if (res.apply.id_card_start_day) {
        let set = this.setFiltering(res.apply.id_card_start_day)
        this.setData({
          'form.id_card_start_day': res.apply.id_card_start_day,
          startTime: res.apply.id_card_start_day
        })
      }
      if (res.apply.id_card_end_day) {
        let set = this.setFiltering(res.apply.id_card_end_day)
        this.setData({
          'form.id_card_end_day': res.apply.id_card_end_day,
          endTime: res.apply.id_card_end_day
        })
      }
    }
  },
  setFiltering(str) {
    let year = str.slice(0, 4);
    let month = str.slice(4, 6);
    let day = str.slice(6, 8);
    return year + "-" + month + "-" + day;
  },
})